package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.DrugInfo;
import com.ruoyi.system.domain.PrescriptionVisit;
import com.ruoyi.system.mapper.DrugInfoMapper;
import com.ruoyi.system.service.IDrugInfoService;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPrescriptionlistMapper;
import com.ruoyi.system.domain.SysPrescriptionlist;
import com.ruoyi.system.service.ISysPrescriptionlistService;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.DrugInfo;

/**
 * 处方信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@Service
public class SysPrescriptionlistServiceImpl implements ISysPrescriptionlistService 
{
    @Autowired
    private SysPrescriptionlistMapper sysPrescriptionlistMapper;
    @Autowired
    private IDrugInfoService drugInfoService;
    @Autowired
    private DrugInfoMapper drugInfoMapper;

    /**
     * 查询处方信息
     * 
     * @param prescriptionId 处方信息主键
     * @return 处方信息
     */
    @Override
    public SysPrescriptionlist selectSysPrescriptionlistByPrescriptionId(Long prescriptionId)
    {
        return sysPrescriptionlistMapper.selectSysPrescriptionlistByPrescriptionId(prescriptionId);
    }

    /**
     * 查询处方信息列表
     * 
     * @param sysPrescriptionlist 处方信息
     * @return 处方信息
     */
    @Override
    public List<SysPrescriptionlist> selectSysPrescriptionlistList(SysPrescriptionlist sysPrescriptionlist)
    {
        return sysPrescriptionlistMapper.selectSysPrescriptionlistList(sysPrescriptionlist);
    }

    /**
     * 新增处方信息
     * 
     * @param sysPrescriptionlist 处方信息
     * @return 结果
     */
    @Override
    public int insertSysPrescriptionlist(SysPrescriptionlist sysPrescriptionlist)
    {
        return sysPrescriptionlistMapper.insertSysPrescriptionlist(sysPrescriptionlist);
    }

    /**
     * 修改处方信息
     * 
     * @param sysPrescriptionlist 处方信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysPrescriptionlist(SysPrescriptionlist sysPrescriptionlist) {
        // 检查是否为“已抓药”状态
        if ("已抓药".equals(sysPrescriptionlist.getDrugStatus())) {
            // 查询药品信息
            DrugInfo drugInfo = drugInfoService.selectDrugInfoByDrugName(sysPrescriptionlist.getDrugName());
            System.out.println(drugInfo.toString());

            if (drugInfo != null) {
                // 减少药品库存
                long newNum = (drugInfo.getNum() - sysPrescriptionlist.getDrugCount());
                //System.out.println(newNum);
                if (newNum >= 0) {
                    drugInfo.setDrugName(sysPrescriptionlist.getDrugName());
                    drugInfo.setNum(sysPrescriptionlist.getDrugCount());
                    //System.out.println(drugInfo.getNum());

                    //System.out.println(sysPrescriptionlist.getDrugName());
                    // 输出日志以确认代码已运行
                    //System.out.println("代码已运行，已减少库存");
                    System.out.println("--------------------------------------------------------------------");
                    System.out.println(drugInfo.getWarnValue());
                    System.out.println(drugInfo.getDrugName());
                    System.out.println(newNum);

                    if (newNum <= drugInfo.getWarnValue()) {
                        System.out.println("-----------------------------------------");
                        drugInfo.setStatus("异常");
                        System.out.println(drugInfo.getStatus());
                    }
                    System.out.println(drugInfo.getStatus());
                    drugInfoMapper.updateDrugInfo(drugInfo);
//                    drugInfoService.updateDrugInfo(drugInfo);
                }  else {
                    throw new IllegalArgumentException("库存不足");
                }
                //if (drugInfo.getWarnValue() != null) {
                    //long warnValue = drugInfo.getWarnValue().longValue();
                    //if (newNum <= warnValue) {
                        //drugInfo.setDrugName(sysPrescriptionlist.getDrugName());
                        //drugInfo.setStatus("异常");
                        //drugInfoMapper.updateDrugInfoNum(drugInfo);
                    //}
                //}
            } else {
                throw new IllegalArgumentException("药品不存在");
            }
        }
        // 更新处方状态
        return sysPrescriptionlistMapper.updateSysPrescriptionlist(sysPrescriptionlist);
    }

    /**
     * 批量删除处方信息
     * 
     * @param prescriptionIds 需要删除的处方信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPrescriptionlistByPrescriptionIds(Long[] prescriptionIds)
    {
        return sysPrescriptionlistMapper.deleteSysPrescriptionlistByPrescriptionIds(prescriptionIds);
    }

    /**
     * 删除处方信息信息
     * 
     * @param prescriptionId 处方信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPrescriptionlistByPrescriptionId(Long prescriptionId)
    {
        return sysPrescriptionlistMapper.deleteSysPrescriptionlistByPrescriptionId(prescriptionId);
    }

    /**
     * 通过就诊id查询姓名
     *
     * @param prescriptionVisit 处方信息主键
     * @return 结果
     */
    @Override
    public List<PrescriptionVisit> selectPatientNameByVisitId(PrescriptionVisit prescriptionVisit) {
        return sysPrescriptionlistMapper.selectPatientNameByVisitId(prescriptionVisit);
    }



}
